cat('Creating summary table...\n\n')
##Use the following csv file for this table
dat=read.csv("summary_stats.csv")

##make table for summary statistics with 5 categories and an extra column for labels and an extra row for each row for labels
summary.table <- data.frame(nrow = 10, ncol = 11)

##Input the statistics (percentages and counts) into the dataframe first for
##cell-combining purposes, combine them, then add labels into the dataframe

##Get the percentage and count of members of each race
summary.table[2,2] <- round(100*(length(which(dat$rec.rac == 'White')))/nrow(dat),1)
summary.table[2,3] <- length(which(dat$rec.rac == 'White'))
summary.table[2,4] <- round(100*(length(which(dat$rec.rac == 'Black')))/nrow(dat),1)
summary.table[2,5] <- length(which(dat$rec.rac == 'Black'))
summary.table[2,6] <- round(100*(length(which(dat$rec.rac == 'Asian')))/nrow(dat),1)
summary.table[2,7] <- length(which(dat$rec.rac == 'Asian'))
summary.table[2,8] <- round(100*(length(which(dat$rec.rac == 'AIAN')))/nrow(dat),1)
summary.table[2,9] <- length(which(dat$rec.rac == 'AIAN'))
summary.table[2,10] <- round(100*(length(which(dat$rec.rac == 'Other')))/nrow(dat),1)
summary.table[2,11] <- length(which(dat$rec.rac == 'Other'))

##Get the percentage and count of each education category
summary.table[4,2] <- round(100*(length(which(dat$rec.edu == 'No.HS')))/nrow(dat),1)
summary.table[4,3] <- length(which(dat$rec.edu == 'No.HS'))
summary.table[4,4] <- round(100*(length(which(dat$rec.edu == 'HS.Diploma')))/nrow(dat),1)
summary.table[4,5] <- length(which(dat$rec.edu == 'HS.Diploma'))
summary.table[4,6] <- round(100*(length(which(dat$rec.edu == 'Some.College')))/nrow(dat),1)
summary.table[4,7] <- length(which(dat$rec.edu == 'Some.College'))
summary.table[4,8] <- round(100*(length(which(dat$rec.edu == 'Undergrad.Degree')))/nrow(dat),1)
summary.table[4,9] <- length(which(dat$rec.edu == 'Undergrad.Degree'))
summary.table[4,10] <- round(100*(length(which(dat$rec.edu == 'Grad.Degree')))/nrow(dat),1)
summary.table[4,11] <- length(which(dat$rec.edu == 'Grad.Degree'))

##Get the percentage and count of each income category
summary.table[6,2] <- round(100*(length(which(dat$five.incomes == 'one')))/nrow(dat),1)
summary.table[6,3] <- length(which(dat$five.incomes == 'one'))
summary.table[6,4] <- round(100*(length(which(dat$five.incomes == 'two')))/nrow(dat),1)
summary.table[6,5] <- length(which(dat$five.incomes == 'two'))
summary.table[6,6] <- round(100*(length(which(dat$five.incomes == 'three')))/nrow(dat),1)
summary.table[6,7] <- length(which(dat$five.incomes == 'three'))
summary.table[6,8] <- round(100*(length(which(dat$five.incomes == 'four')))/nrow(dat),1)
summary.table[6,9] <- length(which(dat$five.incomes == 'four'))
summary.table[6,10] <- round(100*(length(which(dat$five.incomes == 'five')))/nrow(dat),1)
summary.table[6,11] <- length(which(dat$five.incomes == 'five'))

##Get the percentage and count of each Party ID group
summary.table[8,2] <- round(100*(length(which(dat$rec.pol == 'Strong Dem')))/nrow(dat),1)
summary.table[8,3] <- length(which(dat$rec.pol == 'Strong Dem'))
summary.table[8,4] <- round(100*(length(which(dat$rec.pol == 'Democrat')))/nrow(dat),1)
summary.table[8,5] <- length(which(dat$rec.pol == 'Democrat'))
summary.table[8,6] <- round(100*(length(which(dat$rec.pol == 'Independent')))/nrow(dat),1)
summary.table[8,7] <- length(which(dat$rec.pol == 'Independent'))
summary.table[8,8] <- round(100*(length(which(dat$rec.pol == 'Republican')))/nrow(dat),1)
summary.table[8,9] <- length(which(dat$rec.pol == 'Republican'))
summary.table[8,10] <- round(100*(length(which(dat$rec.pol == 'Strong Rep')))/nrow(dat),1)
summary.table[8,11] <- length(which(dat$rec.pol == 'Strong Rep'))

##Get the percentage and count of each ideology group
summary.table[10,2] <- round(100*(length(which(dat$rec.ide == 'Very Lib')))/nrow(dat),1)
summary.table[10,3] <- length(which(dat$rec.ide == 'Very Lib'))
summary.table[10,4] <- round(100*(length(which(dat$rec.ide == 'Liberal')))/nrow(dat),1)
summary.table[10,5] <- length(which(dat$rec.ide == 'Liberal'))
summary.table[10,6] <- round(100*(length(which(dat$rec.ide == 'Neither')))/nrow(dat),1)
summary.table[10,7] <- length(which(dat$rec.ide == 'Neither'))
summary.table[10,8] <- round(100*(length(which(dat$rec.ide == 'Conservative')))/nrow(dat),1)
summary.table[10,9] <- length(which(dat$rec.ide == 'Conservative'))
summary.table[10,10] <- round(100*(length(which(dat$rec.ide == 'Very Con')))/nrow(dat),1)
summary.table[10,11] <- length(which(dat$rec.ide == 'Very Con'))

##Now concatenate the the percentage and the count cells into one column
summary.table[,2] = paste(summary.table[,2], ' (',summary.table[,3],')',sep='')
summary.table[,4] = paste(summary.table[,4], ' (',summary.table[,5],')',sep='')
summary.table[,6] = paste(summary.table[,6], ' (',summary.table[,7],')',sep='')
summary.table[,8] = paste(summary.table[,8], ' (',summary.table[,9],')',sep='')
summary.table[,10] = paste(summary.table[,10], ' (',summary.table[,11],')',sep='')

##Now that the percentages and counts have been concatenated
##you can fill in the rest of the labels for this table

##Label each race category
summary.table[1,2] <- "White"
summary.table[1,4] <- "Black"
summary.table[1,6] <- "Asian"
summary.table[1,8] <- "AIAN"
summary.table[1,10] <- "Other"

##Label each education category
summary.table[3,2] <- "No Diploma"
summary.table[3,4] <- "HS Diploma"
summary.table[3,6] <- "Some College"
summary.table[3,8] <- "Undergrad Degree"
summary.table[3,10] <- "Grad Degree"

##Label each income category
summary.table[5,2] <- "Less than $10K"
summary.table[5,4] <- "$10K-50K"
summary.table[5,6] <- "$50K-100K"
summary.table[5,8] <- "$100K-150K"
summary.table[5,10] <- "More than $150K"

##Label each Party ID group
summary.table[7,2] <- "Strong Dem"
summary.table[7,4] <- "Democrat"
summary.table[7,6] <- "Independent"
summary.table[7,8] <- "Republican"
summary.table[7,10] <- "Strong Rep"

##Label each ideology group
summary.table[9,2] <- "Very Lib"
summary.table[9,4] <- "Liberal"
summary.table[9,6] <- "Neither"
summary.table[9,8] <- "Conservative"
summary.table[9,10] <- "Very Con"

##This creates the first column, which consists of the variable names
##and an indicator that each cell will display both percentages and counts
summary.table[1,1] <- "RACE"
summary.table[2,1] <- "% (count)"
summary.table[3,1] <- "EDUCATION"
summary.table[4,1] <- "% (count)"
summary.table[5,1] <- "INCOME"
summary.table[6,1] <- "% (count)"
summary.table[7,1] <- "PARTY ID"
summary.table[8,1] <- "% (count)"
summary.table[9,1] <- "IDEOLOGY"
summary.table[10,1] <- "% (count)"

##Collapse the count columns now that they are combined
summary.table[,11] = NULL; summary.table[,9] = NULL; summary.table[,7] = NULL; summary.table[,5] = NULL; summary.table[,3] = NULL


#####
##########The following two steps will output a latex table to the current working directory
#####


#########

##Now define a latex table using the xtable function
summary.table1 <- xtable(summary.table,
type = "latex",
file = "summary.table",
caption = "Experiment 4 Summary Statistics",
caption.placement = "top",
include.rownames=FALSE
)

##Now create the file with the filename "TableS13.tex"
print(summary.table1,
include.rownames=FALSE,
include.colnames=FALSE,
file = "TableS13.tex",
floating = F,
booktabs = T,
latex.environments = "left"
)

##The following lets you preview the table in R
print(summary.table1,
include.rownames=FALSE,
include.colnames=FALSE)



